package net.pzc.resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Locale;
import java.util.Properties;

public class Resource {
	private static final String FILE_PREFIX = "ui_"; // 属性文件名的前缀
	private static final String FILE_EXT = ".properties"; // 属性文件名的后缀
	private static final String OUT_FOLDER = "lang"; // 属性文件名的后缀
	private static final Locale DEF_LOCALE = Locale.US;
	private static final String SYS_USER_DIR = System.getProperty("user.dir");
	private static final String SYS_FILE_SEPARATOR = System.getProperty("file.separator");

	private static Resource _instance = null;

	private Properties paras = null;

	public static String get(String key) {
		if (_instance == null) {
			_instance = new Resource();
			synchronized (_instance) {
				Locale locale = Locale.getDefault(); // 获取系统的区域与语言默认设置
				log("Locale:" + locale);
				try {
					_instance.init(locale);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return _instance.getRes(key);
	}

	/* 以下是根据传入的属性文件中的"键",而得到与区域与语言设置相对应的"值" */
	private String getRes(String key) {
		return paras.getProperty(key);
	}

	private void init(Locale locale) throws Exception {
		paras = new Properties();

		final String localFileName = FILE_PREFIX + locale + FILE_EXT;
		final String defLocalFileName = FILE_PREFIX + DEF_LOCALE + FILE_EXT;

		InputStream is = null;
		String path = "";
		File file = null;
		// 1.out locale file
		path = SYS_USER_DIR + SYS_FILE_SEPARATOR + OUT_FOLDER + SYS_FILE_SEPARATOR + localFileName;
		file = getFile(path);
		if (file == null) {
			// 2.out default locale file
			path = SYS_USER_DIR + SYS_FILE_SEPARATOR + OUT_FOLDER + SYS_FILE_SEPARATOR + defLocalFileName;
			file = getFile(path);

			// 3.eclipse locale file
			final URL localeUrl = Resource.class.getResource("/" + OUT_FOLDER + "/" + localFileName);
			if (file == null && localeUrl != null) {
				path = localeUrl.toURI().toString();
				if (!path.startsWith("jar:file:")) {
					path = localeUrl.getFile();
					file = getFile(path);
				}
			}

			// 4.eclipse default locale file
			final URL defLocaleUrl = Resource.class.getResource("/" + OUT_FOLDER + "/" + defLocalFileName);
			if (file == null && defLocaleUrl != null) {
				path = defLocaleUrl.toURI().toString();
				if (!path.startsWith("jar:file:")) {
					path = defLocaleUrl.getFile();
					file = getFile(path);
				}
			}
		}

		if (file != null && file.exists()) {
			is = new FileInputStream(file);
		} else {
			is = getClassResStream(localFileName);

			if (is == null) {
				is = getClassResStream(defLocalFileName);
			}
		}

		if (is != null) {
			paras.load(is);
			is.close();
		} else {
			throw new Exception("local file is not exists!!");
		}
	}

	private File getFile(String path) {
		log(path);
		File ret = new File(path);
		if (ret.exists()) {
			log("exists");
		} else {
			log("not exists");
			ret = null;
		}
		return ret;
	}

	private InputStream getClassResStream(String name) throws Exception {
		log(name);
		InputStream is = Resource.class.getResourceAsStream("/" + OUT_FOLDER + "/" + name);
		if (is != null) {
			log("exists");
		} else {
			log("not exists");
		}
		return is;
	}

	public static void main(String[] args) {
		log(Resource.get("label.file_name"));
	}

	private static void log(Object o) {
		System.out.println(o);
	}

}
